module repository.GroupRepository;

import bootstrap.DatabaseBootstrap;
import hunt.entity;
import hunt.logging;
import std.conv : to;
import model;
import repository;
import message.MsgDefine;
import service.IdGeneratorService;

import hunt.framework;

class GroupRepository : EntityRepository!( ChatGroup , int){

  this()
  {
    super(defaultEntityManager());
  }

  ChatGroup[] getAll()
  {
    return _manager.createQuery!(ChatGroup)("select * from ChatGroup").getResultList();
  }

  ChatGroup findByUid(int groupId )
  {
    return find(new Condition("%s = %s" ,Field.id , groupId));
  }

  int addGroup(CreateGroupREQ req , EntityManager em)
  {
    //scope(exit)
    //{
    //  _manager.close();
    //}

   // _manager.createNativeQuery("SET auto_increment_offset =2;SET auto_increment_increment=2;");
    string id = IdGeneratorService.instance().getNextId();
    auto insert = em.createQuery!(ChatGroup)("INSERT INTO ChatGroup u (u.manager,u.aliasName,u.created,u.validation,u.notice,u.id) VALUES (:members,:groupName,:createTime,:validation,:notice,:id);");
    insert.setParameter("members",to!int(req.users[0]));
    insert.setParameter("groupName",req.groupName);
    insert.setParameter("createTime",req.createTime);
    insert.setParameter("validation",0);
    insert.setParameter("notice","");
    insert.setParameter("id",to!uint(id));
    insert.exec();
    //RowSet rowset = em.createNativeQuery("SELECT MAX(id) FROM im_group;").getResultList();
    //
    //Row row =  rowset.iterator.front();
    return to!uint(id);
  }

  bool isCreator(string fromId, string groupId)
  {
      ChatGroup groupInfo = findByUid(to!int(groupId));
      return fromId == to!string(groupInfo.manager);
  }




  bool needValidation(string groupId , string userId , out string manager)
  {
      ChatGroup groupInfo = findByUid(to!int(groupId));
      manager = to!string(groupInfo.manager);
      return (groupInfo.validation == 0 ||  userId == to!string(groupInfo.manager)) ? false : true;
  }

  void deleteGroup(string groupId ,EntityManager em)
  {
    auto update = em.createQuery!(ChatGroup)("DELETE FROM ChatGroup u WHERE u.id = :groupId");
    update.setParameter("groupId",to!uint(groupId));
    try{
      update.exec();
    }
    catch(Exception e)
    {
      logError("deleteGroup error : %s",e.msg);
    }
  }

  void setValidation(uint validation, string groupId, string userId)
  {
    scope(exit)
    {
      _manager.close();
    }
    auto update = _manager.createQuery!(ChatGroup)("UPDATE ChatGroup u SET u.validation = :validation WHERE u.id = :groupId and u.manager = :manager");
    update.setParameter("validation",validation);
    update.setParameter("groupId",to!uint(groupId));
    update.setParameter("manager",to!uint(userId));
    try{
      update.exec();
    }
    catch(Exception e)
    {
      logError("setValidation error : %s",e.msg);
    }
  }


  void changeManager(string groupId, string userId, EntityManager em)
  {
      auto update = _manager.createQuery!(ChatGroup)("UPDATE ChatGroup u SET u.manager = :manager WHERE u.id = :groupId");
      update.setParameter("manager",to!uint(userId));
      update.setParameter("groupId",to!uint(groupId));
      try{
        update.exec();
      }
      catch(Exception e)
      {
        logError("changeManager error : %s",e.msg);
      }
  }

  void setGroupName(string groupId, string groupName)
  {
      scope(exit)
      {
        _manager.close();
      }
      auto update = _manager.createQuery!(ChatGroup)("UPDATE ChatGroup u SET u.aliasName = :aliasName WHERE u.id = :groupId");
      update.setParameter("aliasName",groupName);
      update.setParameter("groupId",to!uint(groupId));
      try{
        update.exec();
      }
      catch(Exception e)
      {
        logError("setGroupName error : %s",e.msg);
      }
  }

  void updateGroupInfo(UpdateGroupInfo req)
  {
    scope(exit)
    {
      _manager.close();
    }
    auto update = _manager.createQuery!(ChatGroup)("UPDATE ChatGroup u SET u.aliasName = :aliasName, u.avatar = :avatar, u.notice = :notice, u.miniature = :miniatureWHERE u.groupId = :groupId");
    update.setParameter("aliasName",req.aliasName);
    update.setParameter("avatar",req.avatar);
    update.setParameter("notice",req.notice);
    update.setParameter("miniature",req.miniature);
   // update.setParameter("img",cast(ubyte[])req.img);
    update.setParameter("groupId",req.groupId);
    try{
      update.exec();
    }
    catch(Exception e)
    {
      logError("updateGroupInfo error : %s",e.msg);
    }
  }

}

